package com.ruoyi.project.wechatprogram.comment.controller;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.wechatprogram.comment.domain.WxComment;
import com.ruoyi.project.wechatprogram.comment.service.IWxCommentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 订单评论信息Controller
 * 
 * @author sxchen
 * @date 2020-04-21
 */
@Controller
@RequestMapping("/wechatprogram/comment")
public class WxCommentController extends BaseController
{
    private String prefix = "wechatprogram/comment";

    @Autowired
    private IWxCommentService wxCommentService;

    @RequiresPermissions("wechatprogram:comment:view")
    @GetMapping()
    public String comment()
    {
        return prefix + "/comment";
    }

    /**
     * 查询订单评论信息列表
     */
    @PostMapping("/wxlist")
    @ResponseBody
    public TableDataInfo wxlist(WxComment wxComment)
    {
        startPage();
        List<WxComment> list = wxCommentService.selectWxCommentList(wxComment);
        return getDataTable(list);
    }

    /**
     * 查询订单评论信息列表
     */
    @RequiresPermissions("wechatprogram:comment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxComment wxComment)
    {
        startPage();
        List<WxComment> list = wxCommentService.selectWxCommentList(wxComment);
        return getDataTable(list);
    }

    /**
     * 导出订单评论信息列表
     */
    @RequiresPermissions("wechatprogram:comment:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxComment wxComment)
    {
        List<WxComment> list = wxCommentService.selectWxCommentList(wxComment);
        ExcelUtil<WxComment> util = new ExcelUtil<WxComment>(WxComment.class);
        return util.exportExcel(list, "comment");
    }

    /**
     * 新增订单评论信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单评论信息
     */
    @Log(title = "订单评论信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxComment wxComment)
    {
        if(StrUtil.isBlankIfStr(wxComment)){
            return AjaxResult.error("评价异常！");
        }
        int i = wxCommentService.insertWxComment(wxComment);
        if (i>0){
            return AjaxResult.success("评价成功！");
        }
        return AjaxResult.error("评价失败！");
    }

    /**
     * 修改订单评论信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        WxComment wxComment = wxCommentService.selectWxCommentById(id);
        mmap.put("wxComment", wxComment);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单评论信息
     */
    @RequiresPermissions("wechatprogram:comment:edit")
    @Log(title = "订单评论信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxComment wxComment)
    {
        return toAjax(wxCommentService.updateWxComment(wxComment));
    }

    /**
     * 删除订单评论信息
     */
    @RequiresPermissions("wechatprogram:comment:remove")
    @Log(title = "订单评论信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wxCommentService.deleteWxCommentByIds(ids));
    }
}
